package com.peakmain.wanandroid.mymodule.presenter;

import android.annotation.SuppressLint;

import com.peakmain.wanandroid.mymodule.contract.GankContract;
import com.peakmain.wanandroid.netmodule.base.BasePresenter;
import com.peakmain.wanandroid.netmodule.bean.gank.GankBaseBean;
import com.peakmain.wanandroid.netmodule.bean.gank.GankImageBean;
import com.peakmain.wanandroid.netmodule.net.ApiService;
import com.peakmain.wanandroid.netmodule.net.RetrofitManager;
import com.peakmain.wanandroid.netmodule.net.RxSchedulers;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.functions.Consumer;

/**
 * author: peakmain
 * createdata：2019/7/26
 * mail: 2726449200@qq.com
 * desiption:
 */
public class GankPresenter extends BasePresenter<GankContract.View> implements GankContract.Presenter{
    @Inject
    public GankPresenter(){

    }
    @SuppressLint("CheckResult")
    @Override
    public void getGankImageData(String category, int count) {
        mView.showLoading();
        RetrofitManager.createGank(ApiService.class).getGankImageData(category,count)
                .compose(mView.<GankBaseBean<List<GankImageBean>>>bindToLife())
                .compose(RxSchedulers.<GankBaseBean<List<GankImageBean>>>applySchedulers())
                .subscribe(new Consumer<GankBaseBean<List<GankImageBean>>>() {
                    @Override
                    public void accept(GankBaseBean<List<GankImageBean>> dataResponse) throws Exception {
                        if (!dataResponse.isError()) {
                            mView.showGankImageSuccess(dataResponse.getResults());
                        } else {
                            mView.showFaild("获取数据失败");
                        }
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showFaild(throwable.getMessage());
                        mView.hideLoading();
                    }
                });
    }
}
